package juc.completablefuture.methods.or;

/**
 * @ClassName RunAfterEitherExample
 * @PackageName completablefuture.methods.or
 * @projectName myThread
 * @Description 
 * @Author games
 * @Date 2025/8/27 下午5:26
 * @Version 1.0
 */
import java.util.concurrent.CompletableFuture;

/**
 * 作用：任意一个任务执行完成，进行下一步操作（Runnable）。
 * 返回值：void。
 */
public class RunAfterEitherExample {
    public static void main(String[] args) {
        CompletableFuture<String> cf1 = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "Slow Task";
        });

        CompletableFuture<String> cf2 = CompletableFuture.supplyAsync(() -> "Fast Task");

        cf1.runAfterEither(cf2, () -> System.out.println("One of the tasks completed")); // 输出: One of the tasks completed
    }
}